<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\blogs;
use GrahamCampbell\Markdown\Facades\Markdown;

class bloggerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   

        $blogs_obj = new blogs;

        $data = $blogs_obj -> all();

        return view('blogs.index',['data' => $data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('blogs.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|unique:blogs|max:255',
            'content' => 'required'
        ],[
            'title.required' => '标题必须填',
            'title.unique' => '标题必须唯一',
            'title.max' => '标题超限',
            'content.required' => '内容必须填'
        ]);

        //获取数据
        $res = $request -> except('_token');

        $data = new blogs();

        foreach($res as $key => $value){

            $data -> $key = $value;
        }

        //判断插入成功后跳传
        if($data -> save()){    
            return redirect('/blogger');
        } else {
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $data = new blogs();
        $data = $data -> findOrfail($id);
        //显示markdown格式
        $data['content']= Markdown::convertToHtml($data -> content);
        return view('blogs.show',['data' => $data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {   
        $data = blogs::findOrfail($id);
        return view('blogs.edit',['data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $this->validate($request, [
            'title' => 'required|max:255',
            'content' => 'required'
        ],[
            'title.required' => '标题必须填',
            'title.max' => '标题字数超限',
            'content.required' => '内容必须填'
        ]);        

        //获取数据
        $data = $request -> except('_token','_method');
        $blogs_obj = blogs::findOrfail($id);

        //执行数据重写
        foreach($data as $key => $value){
            $blogs_obj -> $key = $value;
        }

        //跳转
        if($blogs_obj -> save()){    
            return redirect('/blogger');
        } else {
            return back();
        }       

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $data = blogs::findOrfail($id);
        if($data ->delete()){    
            return redirect('/blogger');
        } else {
            return back();
        }          
    }
}
